<?php
namespace app\admin\controller;
use think\Db;
class Ad extends Base{
    public function adList(){
        $list=Db::name('ad')->order('id desc')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function state_off(){
    	$id=input('param.id');
        $status = Db::name('ad')->where(array('id'=>$id))->update(['status'=>2]);
        if($status){
            return json(['code' => 200, 'data' =>$status, 'msg' => '已禁止']);
        }else{
            return json(['code' => 500, 'data' =>$status, 'msg' => '出错']);
        }
    }
    public function state_on(){
    	$id=input('param.id');
        $status = Db::name('ad')->where(array('id'=>$id))->update(['status'=>1]);
        if($status){
            return json(['code' => 200, 'data' =>$status, 'msg' => '已开启']);
        }else{
            return json(['code' => 500, 'data' =>$status, 'msg' => '出错']);
        }
    }
    public function ad_add(){
    	if(request()->isGet()){
    		return $this->fetch();
    	}elseif(request()->isPost()){
            $param = input('post.');
            $data['title']=$param['title'];
            $data['img_url']=$param['img_url'];
            if (!empty($param['imgs'])) {
                $imgbases64=explode(',',$param['imgs']);
                $imgbase64=base64_decode($imgbases64[1]);
                $imgUrld=date('Ymd',time());
                $imgUrls=date('His',time());
                if(!file_exists("../public/uploads/images/".$imgUrld)){
                    mkdir("../public/uploads/images/".$imgUrld,0777);
                }
                chmod("../public/uploads/images/".$imgUrld,0777);
                file_put_contents("../public/uploads/images/".$imgUrld."/".$imgUrls.".jpg",$imgbase64);
                $data['img']=$imgUrld.'/'.$imgUrls.'.jpg';
            }
            $data['time']=time();
            if($param['status']='on'){
                $data['status']=1;
            }elseif($param['status']='off'){
                $data['status']=2;
            }
            $res=Db::name('ad')->insert($data);
            return json(['code' => 200, 'data' =>$res, 'msg' => '调试']);
    	}
    }
    public function ad_edit(){
    	if(request()->isGet()){
			$param = input('get.');
			$user=Db::name('ad')->where('id',$param['id'])->find();
	        $this->assign('user',$user);
    		return $this->fetch();
    	}elseif(request()->isPost()){
			$param = input('post.');
            if (!empty($param['imgs'])) {
                $imgbases64=explode(',',$param['imgs']);
                $imgbase64=base64_decode($imgbases64[1]);
                $imgUrld=date('Ymd',time());
                $imgUrls=date('His',time());
                if(!file_exists("../public/uploads/images/".$imgUrld)){
                    mkdir("../public/uploads/images/".$imgUrld,0777);
                }
                chmod("../public/uploads/images/".$imgUrld,0777);
                file_put_contents("../public/uploads/images/".$imgUrld."/".$imgUrls.".jpg",$imgbase64);
                $data['img']=$imgUrld.'/'.$imgUrls.'.jpg';
            }
            $data['title']=$param['title'];
            $data['img_url']=$param['img_url'];
			$data['time']=time();
			if($param['status']='on'){
				$data['status']=1;
			}elseif($param['status']='off'){
				$data['status']=2;
			}
        	$res= Db::name('ad')->where(array('id'=>$param['id']))->update($data);
            return json(['code' => 200, 'data' =>$res, 'msg' => '更新成功']);
    	}
    }
    public function ad_del(){
		$param = input('post.');
        $res= Db::name('ad')->where(array('id'=>$param['id']))->delete();
        return json(['code' => 200, 'data' =>$res, 'msg' => '删除成功']);

    }
    public function ad_dels(){
		$param = input('post.');
        $res= Db::name('ad')->where(array('id'=>$param['id']))->delete();
        return json(['code' => 200, 'data' =>$res, 'msg' => '删除成功']);

    }
}
